package com.yn.module.spider.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yn.module.spider.config.strategy.SpiderConfig;
import com.yn.module.spider.mapper.NatmcMedicareMaterialMapper;
import com.yn.module.spider.pojo.NatmcMedicareMaterial;
import com.yn.module.spider.utils.base.BaseService;
import com.yn.module.spider.utils.base.MainSpiderService;
import com.yn.module.spider.utils.constant.OptEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@SpiderConfig(svc = OptEnum.SpiderOpertion_NationMedicare_MedicareMaterial)
@Slf4j
@Transactional
public class NatmcMedicareMaterialService extends BaseService<NatmcMedicareMaterialMapper, NatmcMedicareMaterial> implements MainSpiderService {

    /* 主表：删除当前最大页数据 */
    public void deleteCurrentData(Map<String, Object> params) {
        int maxPage = queryCurrentMaxPage((String)params.get(DATA_VERSION));
        if (maxPage != 0) {
            LambdaQueryWrapper<NatmcMedicareMaterial> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NatmcMedicareMaterial::getPage, maxPage)
                    .eq(NatmcMedicareMaterial::getDataVer,params.get(DATA_VERSION));
            this.remove(queryWrapper);
        }

    }

    @Override
    public void saveData2Db(String json, Map<String, Object> selfParams) throws Exception {
        JSONObject jsonObject = JSON.parseObject(json);
        log.info("[current page]-->{},[total pages]-->{},[total records]-->{}", (Integer) selfParams.get(CURRENT_PAGE), jsonObject.get("total"), jsonObject.get("records"));
        Object rows = jsonObject.get("rows");
        if (rows != null) {
            List<NatmcMedicareMaterial> dataList = JSON.parseArray(rows.toString(), NatmcMedicareMaterial.class, JSONReader.Feature.FieldBased);

            int length = dataList.size();
            for (int i = 0; i < length; i++) {
                NatmcMedicareMaterial obj = dataList.get(i);
                obj.setEid(IdWorker.getId());
                obj.setTs(new Date());
                obj.setPage((Integer) selfParams.get(CURRENT_PAGE));
                obj.setLine(i + 1);
                obj.setDataVer((String)selfParams.get(DATA_VERSION));
            }
            this.saveBatch(dataList);
        }
    }

    @Override
    public void setParams(Object mainObj, Map<String, Object> httpParams, Map<String, Object> selfParams) {

        // 设置http请求参数
        httpParams.put("rows", 50);
        httpParams.put("page", httpParams.get(CURRENT_PAGE));
        httpParams.put("sord", "asc");
        httpParams.put("releaseVersion", "20220215");
        httpParams.put("_search", false);
        // 设置保存的其它参数
        selfParams.put(CURRENT_PAGE, httpParams.get(CURRENT_PAGE));
        selfParams.put(DATA_VERSION,httpParams.get("releaseVersion"));

    }


    @Override
    public Integer queryCurrentMaxPage(String dataVersion) {
        LambdaQueryWrapper<NatmcMedicareMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NatmcMedicareMaterial::getDataVer,dataVersion).orderByDesc(NatmcMedicareMaterial::getPage)
                .last(" limit 1 ");
        List<NatmcMedicareMaterial> dataList = this.list(queryWrapper);
        if (ObjectUtils.isEmpty(dataList)) return 0;
        return dataList.get(0).getPage();
    }
}
